from flask import Flask
import config
from exts import db, mail, cache, csrf, avatars, jwt, cors
from flask_migrate import Migrate
from models import auth, post
from apps.front import front_bp
from bbs_celery import make_celery
from apps.media import media_bp
import commands
from apps.cmsapi import cmsapi_bp

app = Flask(__name__)
app.config.from_object(config)

db.init_app(app)
mail.init_app(app)
cache.init_app(app)
csrf.init_app(app)
avatars.init_app(app)
jwt.init_app(app)
cors.init_app(app, resources={r"/cmsapi/*": {"origins": "*"}})


# celery -A app.mycelery worker --loglevel=info -P gevent

# 排除cmsapi的csrf验证
csrf.exempt(cmsapi_bp)

migrate = Migrate(app, db)

mycelery = make_celery(app)

# 注册蓝图
app.register_blueprint(front_bp)
app.register_blueprint(media_bp)
app.register_blueprint(cmsapi_bp)

# 注册命令
app.cli.command('init_boards')(commands.init_boards)
# app.cli.command('create_test_posts')(commands.create_test_posts)
app.cli.command('init_roles')(commands.init_roles)
# app.cli.command('bind_roles')(commands.bind_roles)
app.cli.command('init_developor')(commands.init_developor)

if __name__ == '__main__':
    app.run(host='0.0.0.0')
